﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Things.Common;

namespace Things.ThingsWeb.Components
{
    public partial class BuyerAddressAdd : System.Web.UI.UserControl
    {
        protected Guid BuyerAddressID
        {
            get
            {
                Guid temp = Guid.Empty;
                if (Request.Params["ID"] != null && Request.Params["ID"].Trim() != "")
                {
                    if (Guid.TryParse(Request.Params["ID"], out temp) == false)
                    {
                        temp = Guid.Empty;
                    }
                }

                return temp;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (BuyerAddressID != Guid.Empty)
                {
                    ShowInfo(BuyerAddressID);
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            Things.Model.Buyer BuyerAccount = Session[ConstantString.SessionCurrentBuyerAccount] as Things.Model.Buyer;
            if (BuyerAccount == null)
            {
                strErr += "帐号信息有误！\\n";
            }
            if (this.txtBuyerAddressName.Text.Trim().Length == 0)
            {
                strErr += "收货地址不能为空！\\n";
            }
            if (this.txtZipCode.Text.Trim().Length == 0)
            {
                strErr += "邮政编码不能为空！\\n";
            }
            if (this.txtReceiverName.Text.Trim().Length == 0)
            {
                strErr += "收件人不能为空！\\n";
            }
            if (this.txtReceiverMobile.Text.Trim().Length == 0)
            {
                strErr += "手机号码不能为空！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this.Page, strErr);
                return;
            }
           

            string BuyerAddressName = this.txtBuyerAddressName.Text;
            bool IsDefault = this.chkIsDefault.Checked;
            string ZipCode = this.txtZipCode.Text;
            string BuyerAddressCity = this.ddlBuyerAddressCity.SelectedValue;
            string ReceiverName = this.txtReceiverName.Text;
            string ReceiverMobile = this.txtReceiverMobile.Text;
            string ReceiverTelephone = this.txtReceiverTelephone.Text;



            Things.BLL.BuyerAddress bll = new Things.BLL.BuyerAddress();
            Things.Model.BuyerAddress buyerAddress = bll.GetModel(BuyerAddressID);

            bool flag = false;
            if (buyerAddress != null)
            {
                buyerAddress.BuyerAddressName = BuyerAddressName;
                buyerAddress.IsDefault = IsDefault;
                buyerAddress.ZipCode = ZipCode;
                buyerAddress.BuyerAddressCity = BuyerAddressCity;
                buyerAddress.ReceiverName = ReceiverName;
                buyerAddress.ReceiverMobile = ReceiverMobile;
                buyerAddress.ReceiverTelephone = ReceiverTelephone;

                if(IsDefault==true)
                {
                    bll.UpdateDefaultAddress();
                }
                flag = bll.Update(buyerAddress);
            }
            else
            {
                Things.Model.BuyerAddress model = new Things.Model.BuyerAddress();
                model.BuyerAccount = BuyerAccount.Account;
                model.BuyerAddressName = BuyerAddressName;
                model.IsDefault = IsDefault;
                model.ZipCode = ZipCode;
                model.BuyerAddressCity = BuyerAddressCity;
                model.ReceiverName = ReceiverName;
                model.ReceiverMobile = ReceiverMobile;
                model.ReceiverTelephone = ReceiverTelephone;

                if (IsDefault == true)
                {
                    bll.UpdateDefaultAddress();
                }

                flag = bll.Add(model);
            }

            if (flag == true)
            {
                MessageBox.ResponseScript(this.Page, "window.parent.$.colorbox.close(); ");
                //Response.Redirect("../GoodsOrders/GoodsOrder.aspx");
            }
            else
            {
            }

        }


        private void ShowInfo(Guid BuyerAddressID)
        {
            Things.BLL.BuyerAddress bll = new Things.BLL.BuyerAddress();
            Things.Model.BuyerAddress model = bll.GetModel(BuyerAddressID);
            this.lblBuyerAddressID.Text = model.BuyerAddressID.ToString();
            this.txtBuyerAddressName.Text = model.BuyerAddressName;
            this.chkIsDefault.Checked = model.IsDefault;
            this.txtZipCode.Text = model.ZipCode;
            this.ddlBuyerAddressCity.SelectedValue = model.BuyerAddressCity;
            this.txtReceiverName.Text = model.ReceiverName;
            this.txtReceiverMobile.Text = model.ReceiverMobile;
            this.txtReceiverTelephone.Text = model.ReceiverTelephone;

        }


        protected void btnCancle_Click(object sender, EventArgs e)
        {

        }

    }
}